{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b29e4e6e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Pregnancies</th>\n",
       "      <th>Glucose</th>\n",
       "      <th>BloodPressure</th>\n",
       "      <th>SkinThickness</th>\n",
       "      <th>Insulin</th>\n",
       "      <th>BMI</th>\n",
       "      <th>DiabetesPedigreeFunction</th>\n",
       "      <th>Age</th>\n",
       "      <th>Outcome</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>6</td>\n",
       "      <td>148</td>\n",
       "      <td>72</td>\n",
       "      <td>35</td>\n",
       "      <td>0</td>\n",
       "      <td>33.6</td>\n",
       "      <td>0.627</td>\n",
       "      <td>50</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>29</td>\n",
       "      <td>0</td>\n",
       "      <td>26.6</td>\n",
       "      <td>0.351</td>\n",
       "      <td>31</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>8</td>\n",
       "      <td>183</td>\n",
       "      <td>64</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>23.3</td>\n",
       "      <td>0.672</td>\n",
       "      <td>32</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>89</td>\n",
       "      <td>66</td>\n",
       "      <td>23</td>\n",
       "      <td>94</td>\n",
       "      <td>28.1</td>\n",
       "      <td>0.167</td>\n",
       "      <td>21</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>137</td>\n",
       "      <td>40</td>\n",
       "      <td>35</td>\n",
       "      <td>168</td>\n",
       "      <td>43.1</td>\n",
       "      <td>2.288</td>\n",
       "      <td>33</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Pregnancies  Glucose  BloodPressure  SkinThickness  Insulin   BMI  \\\n",
       "0            6      148             72             35        0  33.6   \n",
       "1            1       85             66             29        0  26.6   \n",
       "2            8      183             64              0        0  23.3   \n",
       "3            1       89             66             23       94  28.1   \n",
       "4            0      137             40             35      168  43.1   \n",
       "\n",
       "   DiabetesPedigreeFunction  Age  Outcome  \n",
       "0                     0.627   50        1  \n",
       "1                     0.351   31        0  \n",
       "2                     0.672   32        1  \n",
       "3                     0.167   21        0  \n",
       "4                     2.288   33        1  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "#Read the dataset\n",
    "mydata = pd.read_csv('C:\\\\Users\\\\saiye\\Downloads\\\\ML ProjectTest\\\\diabetes.csv')\n",
    "mydata.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e431df9b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "05a2ab0a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(768, 8)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#providing the input features, X= predictors,regressors, indep, variables\n",
    "X = np. array(mydata[['Pregnancies','Glucose','BloodPressure','SkinThickness','Insulin','BMI','DiabetesPedigreeFunction','Age']])\n",
    "X.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1983c3a2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(768,)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#providing the output feature, y: regressand, dep.variable,response variable\n",
    "y = np.array(mydata['Outcome'])\n",
    "y.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "174ad9f4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(231, 8)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#splitting the dataset into train and test datasets\n",
    "#importing the train test split method\n",
    "from sklearn.model_selection import train_test_split\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.30)\n",
    "X_test.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "bdd587c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.18482672, 0.21996907, 0.25546072, 0.22844843, 0.37304137,\n",
       "       0.24315094, 0.27150243, 0.3503144 , 0.2250272 , 0.17697749])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#The model\n",
    "#Importing the LinearRegression class\n",
    "from sklearn.linear_model import LinearRegression\n",
    "#Instantiation\n",
    "model = LinearRegression()\n",
    "#Fitting the model\n",
    "mymodel = model.fit(X_train, y_train)\n",
    "#K-fold Cross Validation\n",
    "# Importing cross_val_score function\n",
    "from sklearn.model_selection import cross_val_score\n",
    "#10-fold CV\n",
    "scores = cross_val_score(mymodel, X_train, y_train, scoring='r2', cv=10)\n",
    "scores"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b075bd4d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.2528718772623971\n"
     ]
    }
   ],
   "source": [
    "#Printting the average score\n",
    "print(np.mean(scores))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "69a25247",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 3.09396886e-01,  4.88879750e-01,  2.67869483e-01,  5.79169384e-02,\n",
       "        5.95092105e-01,  3.70745503e-01,  1.91428339e-01,  4.08167861e-01,\n",
       "        5.84481758e-01,  7.35896373e-01,  1.19214103e-02,  5.44499070e-01,\n",
       "        5.10437728e-01,  5.42775958e-01,  3.50897631e-01, -8.60752568e-02,\n",
       "        8.63214872e-01,  7.10327326e-01,  1.24348930e-01,  8.42107637e-01,\n",
       "        4.50926809e-01, -3.30592012e-02,  3.54035368e-01,  1.82650667e-01,\n",
       "        7.91765477e-01,  5.29344527e-01,  4.92771666e-01,  6.18937713e-01,\n",
       "        4.20224704e-01,  3.79936839e-01, -7.35592091e-02,  5.28691684e-01,\n",
       "        4.24248498e-01,  9.84545327e-02,  1.78191434e-01,  4.28101279e-01,\n",
       "        6.33325305e-02,  2.30338227e-01,  6.24521397e-01,  2.66320681e-01,\n",
       "        2.68642694e-01,  1.33659487e-01,  8.63666294e-01,  1.20933810e-01,\n",
       "        3.64280426e-01,  5.45743941e-01,  3.58291611e-01, -3.34966655e-02,\n",
       "        4.04085820e-01,  2.56140978e-01,  5.16607313e-01,  3.96531211e-01,\n",
       "       -5.01147609e-02,  4.00447703e-01,  2.97419605e-01,  2.60137192e-01,\n",
       "        7.01776410e-01,  2.46611621e-01,  3.46141555e-01,  1.78153615e-01,\n",
       "       -4.42644686e-01,  3.65364109e-01,  1.30076591e-01,  9.28242225e-01,\n",
       "        8.04443686e-01,  9.05501865e-01,  2.89169531e-01,  1.02103034e-01,\n",
       "        1.66435235e-01,  5.67884045e-01, -2.66115700e-02,  3.53863784e-01,\n",
       "        4.08076910e-01,  7.72547795e-01,  7.66603254e-01,  7.72292410e-01,\n",
       "        2.18036767e-01,  8.22008609e-02,  4.19969485e-01,  2.52329665e-01,\n",
       "        9.09934366e-02,  2.76208178e-01,  1.56125900e-01,  2.28592031e-01,\n",
       "        2.19598892e-01,  6.09901233e-01,  6.86282306e-01,  8.25396834e-01,\n",
       "        4.45314330e-01,  2.97746430e-01,  2.37799555e-01,  5.43352623e-02,\n",
       "        1.30297364e-01,  6.85815154e-01,  1.69140182e-01,  6.85583278e-01,\n",
       "        4.62255746e-01,  8.79782847e-01,  6.36716692e-01,  8.16392531e-01,\n",
       "        3.07376752e-01,  3.37897603e-01,  6.55863128e-01,  3.86021652e-01,\n",
       "        8.00389269e-01,  4.80884203e-01,  7.86414913e-01,  2.24901844e-01,\n",
       "        5.69802885e-02,  8.74657769e-01,  2.23401555e-01,  3.17653422e-01,\n",
       "        4.44762592e-01,  4.47713901e-01,  1.41832993e+00,  1.80879759e-01,\n",
       "        2.34932654e-01,  7.04297231e-01,  4.18142446e-01,  2.94853544e-01,\n",
       "        3.59225673e-01,  2.24949258e-01,  4.82204115e-01,  4.91846730e-01,\n",
       "        3.84601607e-01,  5.72807906e-01,  2.73393372e-01,  8.95327282e-01,\n",
       "        7.34645867e-01,  7.20026798e-01,  7.34845557e-01,  1.54051309e-01,\n",
       "        5.32440080e-01,  6.92349067e-01,  4.69072149e-01,  5.71387193e-01,\n",
       "        2.27869538e-01,  2.63863690e-01,  1.30202160e-01, -6.20053737e-02,\n",
       "        3.89138557e-01,  4.81992800e-01,  2.84619183e-01,  6.01849378e-01,\n",
       "        4.50415657e-01,  4.34900108e-01,  4.49338085e-01,  4.75649132e-01,\n",
       "        6.07212787e-01,  5.80205220e-01,  3.23435184e-01,  6.54847992e-01,\n",
       "        1.27876463e-01,  2.42563308e-01,  4.46803422e-01, -4.19780737e-02,\n",
       "        1.76582552e-01,  1.40075862e-01,  6.78766726e-01,  5.40245677e-01,\n",
       "        1.32860407e-03, -1.58681020e-01,  4.52852913e-01,  4.24933027e-02,\n",
       "        2.85837810e-02,  1.86296273e-01,  7.73288444e-02,  5.43564734e-01,\n",
       "        2.64801726e-01,  1.24072874e+00,  8.15734884e-02, -3.08994601e-02,\n",
       "        1.89253975e-01,  6.17447493e-01,  4.54052841e-01,  2.65936622e-01,\n",
       "        7.00602890e-01,  2.99093236e-01,  2.96787508e-01,  4.08522870e-02,\n",
       "        6.19410926e-01,  7.23298895e-01,  6.84095139e-01,  7.94420628e-03,\n",
       "        7.43492582e-01,  9.86237145e-02,  3.55127918e-01,  5.55464208e-01,\n",
       "        1.10917572e-01,  2.52556289e-01,  2.58852382e-01,  3.00058457e-01,\n",
       "        7.42497589e-02, -5.41125269e-02,  7.35592232e-01,  7.74563819e-01,\n",
       "       -1.10552537e-01,  3.86616245e-01,  7.00094100e-01,  2.06910721e-01,\n",
       "        8.26812011e-02,  2.93167918e-02,  4.45680437e-01,  7.25466390e-01,\n",
       "        3.78185790e-01, -3.86652238e-02, -1.81062015e-01,  4.34504630e-01,\n",
       "       -1.76980599e-01,  1.34871422e-01,  1.95673749e-01,  5.41744961e-01,\n",
       "        1.74646503e-01,  4.73516531e-01,  2.67001030e-01,  2.07937213e-01,\n",
       "        9.00496681e-01,  3.43119440e-03, -1.18340930e-01,  2.63802338e-01,\n",
       "        4.68873481e-01,  1.70458513e-01, -1.36095281e-02,  2.20298826e-01,\n",
       "        3.29829007e-01, -1.44000896e-01,  5.88661070e-01,  1.23557956e-02,\n",
       "        1.83026891e-01,  7.43814214e-01,  4.50674998e-02])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Printing the scores on the test dataset\n",
    "#First: Prediction\n",
    "#Importing cross_val_predict function\n",
    "from sklearn.model_selection import cross_val_predict\n",
    "#The predictions\n",
    "pred = cross_val_predict(model, X_test, y_test)\n",
    "pred"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "39c37cdc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.38046991,  0.27442836, -0.02122382,  0.15147421,  0.36515807,\n",
       "        0.30358763,  0.34355002,  0.29798847,  0.3611689 , -0.07368638])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#10-fodl CV on test data\n",
    "scores_test = cross_val_score(model, X_test, y_test, cv=10)\n",
    "scores_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "fc71244f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.23829153705841516\n"
     ]
    }
   ],
   "source": [
    "#The average score\n",
    "print(np.mean(scores_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "822432b0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
